import Joi from 'joi';

export const loginSchema = Joi.object({
  username: Joi.string().required().messages({
    'string.empty': '用户名不能为空',
    'any.required': '请填写用户名'
  }),
  password: Joi.string().required().messages({
    'string.empty': '密码不能为空',
    'any.required': '请填写密码'
  }),
  captcha: Joi.string().length(4).required().messages({
    'string.length': '验证码必须为4位',
    'any.required': '请填写验证码'
  })
});

export const refreshTokenSchema = Joi.object({
  refreshToken: Joi.string().required().messages({
    'string.empty': 'refreshToken不能为空',
    'any.required': '请提供refreshToken'
  })
});

// 用户注册验证模式
export const registerSchema = Joi.object({
  username: Joi.string()
    .min(4)
    .max(16)
    .pattern(/^[a-zA-Z0-9_]+$/)
    .required()
    .messages({
      'string.pattern.base': '用户名只能包含字母、数字和下划线'
    }),

  password: Joi.string()
    .min(6)
    .pattern(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{6,}$/)
    .required()
    .messages({
      'string.pattern.base': '密码必须包含大小写字母和数字'
    }),

  email: Joi.string()
    .email()
    .required(),

  phone: Joi.string()
    .pattern(/^1[3-9]\d{9}$/)
    .required()
    .messages({
      'string.pattern.base': '请输入有效的手机号'
    }),

  captcha: Joi.string()
    .length(6)
    .required()
    .messages({
      'string.length': '验证码长度为6位'
    }),
});

export const captchaSchema = Joi.object({
  phone: Joi.string()
    .pattern(/^1[3-9]\d{9}$/)
    .required()
});